package Command;

import java.util.ArrayList;
import Server.Command;
import Server.cmdArguments;

public class CloseCommand implements Command{

	private cmdArguments args;

	public CloseCommand(){}

	@Override
	public String execute() {
		String result = "";
		if (args.db) {	
			ArrayList<Integer> data = new ArrayList<Integer>();
//			while (!args.ds.isEmpty()) {
//				data.add(args.ds.poll());
//			}
			
			data = args.bst.getSeqList();

			if (args.commands.length > 1 && (args.commands[1].toLowerCase()).equals("nosave")) {
				result = "Closed connection without saving.";
			}
			else {
				args.da.insert(data);
				result = "Closed connection. Data set added to database.";
			}
			args.da.release();


		} else {
			result = "Closed connection.";
		}
		return result;
	}

	public void setArgs(cmdArguments args) {
		this.args=args;
	}

	@Override
	public String undo() {
		return "Undo on close() not possible...";
	}
}